CHNIP='chnip.sh'
VPNDOWN='vpndown.sh'
VPNUP='/tmp/vpnup.sh'
VPNLOG='/tmp/myautovpn.log'
IPUP="/tmp/pptpd_client/ip-up"
IPDOWN="/tmp/pptpd_client/ip-down"
DLDIR='http://my-autovpn.googlecode.com/svn/trunk/'

echo '#!/bin/sh' > $VPNUP
echo 'OLDGW=$(nvram get wan_gateway)' >> $VPNUP
echo 'VPNSRV=$(nvram get pptpd_client_srvip)' >> $VPNUP
echo 'VPNSRVSUB=$(nvram get pptpd_client_srvsub)' >> $VPNUP
echo 'PPTPDEV=$(route -n | grep ^${VPNSRVSUB%.[0-9]*} | awk '\''{print $NF}'\''| head -n 1)' >> $VPNUP
echo 'VPNGW=$(ifconfig $PPTPDEV | grep -Eo "P-t-P:([0-9.]+)" | cut -d: -f2)' >> $VPNUP
echo 'route add -host $VPNSRV gw $OLDGW' >> $VPNUP
echo 'route del default gw $OLDGW' >> $VPNUP
echo 'route add default gw $VPNGW' >> $VPNUP
echo "cd /tmp" >> $VPNUP
echo "wget $DLDIR$CHNIP && chmod +x $CHNIP && ./$CHNIP" >> $VPNUP
chmod +x $VPNUP

for i in 1 2 3 4 5 6 7 8 9 10 11 12
do
if [ -e $IPUP ]; then
sed -ie 's#exit 0#/tmp/vpnup.sh\nexit 0#g' $IPUP
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") $IPUP modified" >> $VPNLOG
break
else
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") $IPUP not exists, sleep 10sec." >> $VPNLOG
sleep 10
fi
done




